import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
import path from "path";
import {loadEnv} from 'vite'
import AutoImport from 'unplugin-auto-import/vite'


export default defineConfig(({mode}) => {
    const env = loadEnv(mode, process.cwd())
    const {VITE_APP_ENV, VITE_APP_BASE_API, VITE_APP_URL} = env

    return {
        base: VITE_APP_ENV === 'production' ? '/vite/' : '/',
        plugins: [
            vue(),
            AutoImport({
                imports: ['vue'],
                dts: 'types/auto-imports.d.ts', // 使用typescript，需要指定生成对应的d.ts文件或者设置为true,生成默认导入d.ts文件
            }),
        ],
        resolve: {
            alias: {
                '@': path.resolve(__dirname, './src'),
            },
            extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue'],
        },
        server: {
            port: 80,
            host: true,
            hmr: true,
            open: true,
            proxy: {
                [VITE_APP_BASE_API]: {
                    target: VITE_APP_URL,
                    changeOrigin: true,
                    rewrite: (p) => p.replace(/^\/dev-api/, ''),
                },
            },
        },
    }
})